<%@ page import= "java.io.*" %>
<%!
     private static final int BUFSIZE = 2048;
	  /**
     *  Sends a file to the ServletResponse output stream.  Typically
     *  you want the browser to receive a different name than the
     *  name the file has been saved in your local database, since
     *  your local names need to be unique.
     *
     *  @param request The request
     *  @param response The response
     *  @param filename The name of the file you want to download.
     *  @param original_filename The name the browser should receive.
     */
    private void doDownload( HttpServletRequest request, HttpServletResponse response,
                             String filename, String content )
        throws IOException
    {
        int                 length   = 0;
        ServletOutputStream op       = response.getOutputStream();
        ServletContext      context  = getServletConfig().getServletContext();

        //
        //  Set the response and go!
        //
        //
        response.setContentType( "application/octet-stream" );
        response.setHeader( "Content-Disposition", "attachment; filename=\"" + filename + "\"" );

        //
        //  Stream to the requester.
        //
        byte[] bbuf = new byte[BUFSIZE];
        InputStream in = new ByteArrayInputStream(content.getBytes("UTF-8"));

        byte[] BOM_UTF8 = { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };
        op.write(BOM_UTF8);
        while ((in != null) && ((length = in.read(bbuf)) != -1))
        {
            op.write(bbuf,0,length);
        }

        in.close();
        op.flush();
        op.close();
    }
%>
<%
	String filename = request.getParameter("file");
	if(request.getAttribute("file")!=null){
		filename=(String)request.getAttribute("file");
	}
	String content=request.getParameter("content");
	if(request.getAttribute("content")!=null){
		content=(String)request.getAttribute("content");
	}
    doDownload(request, response, filename, content);

%>    
